﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Omu.Awesome.Mvc;
using Omu.AwesomeDemo.Core.Model;
using Omu.AwesomeDemo.Core.Repository;

namespace Omu.AwesomeDemo.WebUI.Areas.Veggies.Controllers
{
    public class FruitsLookupController : LookupController
    {
        private readonly IRepo<Fruit> repo;

        public FruitsLookupController(IRepo<Fruit> repo)
        {
            this.repo = repo;
        }

        public ActionResult Search(string search, IEnumerable<int> selected)
        {
            var result = repo.GetAll().Where(o => o.Name.StartsWith(search));
            if (selected != null) result = result.Where(o => !selected.Contains(o.Id));

            return View(@"Awesome\LookupList", result);
        }

        public ActionResult Selected(IEnumerable<int> selected)
        {
            var result = selected != null ? repo.GetAll().Where(o => selected.Contains(o.Id)) : null;
            return View(@"Awesome\LookupList", result);
        }

        public ActionResult GetMultiple(IEnumerable<int> selected)
        {
            return Json(repo.GetAll().Where(o => selected.Contains(o.Id)).Select(o => new { Text = o.Name }));
        }
    }
}